package com.example.slidingfinish;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
	private Context mContext;
	private ArrayList<CartShopEntity> mDataList = new ArrayList<CartShopEntity>();

	public ExpandableListAdapter(Context mContext, ArrayList<CartShopEntity> dataList) {
		super();
		this.mContext = mContext;
		this.mDataList = dataList;
	}
	
	/**
	 * 取得已选择的店铺（及商品）列表
	 * @return
	 */
	public ArrayList<CartShopEntity> getSelectedList(){
		ArrayList<CartShopEntity> selectedShopList = new ArrayList<CartShopEntity>();
		
		// 遍历店铺
		for(CartShopEntity shopEntity : mDataList){
			ArrayList<GoodsEntity> goodsList = shopEntity.getGoodsList();
			ArrayList<GoodsEntity> selectedGoodsList = new ArrayList<GoodsEntity>();
			
			// 遍历商品，如果被选中，则加入店铺已选择商品列表中去
			for(GoodsEntity goodsEntity : goodsList){
				if(goodsEntity.isSelect()){
					selectedGoodsList.add(goodsEntity);
				}
			}
			
			// 如果该店铺的已选择商品数量>0，则将该店铺加入到已选择店铺列表中去
			if(selectedGoodsList.size() > 0){
				selectedShopList.add(shopEntity);
			}
		}
		return selectedShopList;
//		StringBuilder sb = new StringBuilder();
//		for(CartShopEntity shopEntity : selectedShopList){
//			String str = "";
//			ArrayList<GoodsEntity> goodsList = shopEntity.getGoodsList();
//			for(GoodsEntity goodsEntity : goodsList){
//				if(goodsEntity.isSelect()){
//					str += goodsEntity.getGoodsTitle() + "|";
//				}
//			}
//			sb.append(shopEntity.getShopName() + "--" + str + "\n");
//		}
//		
//		Toast.makeText(mContext, sb.toString(), Toast.LENGTH_LONG).show();
	}

	public Object getChild(int groupPosition, int childPosition) {
		return mDataList.get(groupPosition).getGoodsList().get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		return mDataList.get(groupPosition).getGoodsList().size();
	}

	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder holder ;
		
		// 这里是为了处理因groupPosition和childPosition变换造成的checkbox状态选中状态混乱的问题
		final int groupIndex = groupPosition;
		final int childIndex = childPosition;
		
		if(null == convertView){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_shoping_cart_child, null);
			holder = new ChildViewHolder();
			holder.childTitleTv = (TextView) convertView.findViewById(R.id.childTitle);
			holder.childCheckbox = (CheckBox) convertView.findViewById(R.id.childCheckbox);
			convertView.setTag(holder);
		}else{
			holder = (ChildViewHolder) convertView.getTag();
		}
		
		holder.childCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				mDataList.get(groupIndex).getGoodsList().get(childIndex).setSelect(isChecked);
				childSelectAssociatedHanlder(groupIndex);
			}
		});

		GoodsEntity goodsEntity = mDataList.get(groupIndex).getGoodsList().get(childIndex);
		holder.childTitleTv.setText(goodsEntity.getGoodsTitle());
		holder.childCheckbox.setChecked(goodsEntity.isSelect());
		
		return convertView;
	}

	public Object getGroup(int groupPosition) {
		return mDataList.get(groupPosition);
	}

	public int getGroupCount() {
		return mDataList.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(final int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
	    GroupViewHolder holder ;
		final int groupIndex = groupPosition;
		if(null == convertView){
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_shoping_cart_group, null);
			holder = new GroupViewHolder();
			holder.groupTitleTv = (TextView) convertView.findViewById(R.id.groupTitle);
			holder.groupCheckbox = (CheckBox) convertView.findViewById(R.id.groupCheckbox);
			convertView.setTag(holder);
		}else{
			holder = (GroupViewHolder) convertView.getTag();
		}
		
		// 这里不用onCheckedChange是因为childSelectAssociatedHanlder方法里所做的操作
		holder.groupCheckbox.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mDataList.get(groupIndex).setSelect(((CheckBox)v).isChecked());
				groupAllSelect(groupIndex, ((CheckBox)v).isChecked());
			}
		});

		CartShopEntity shopEntity = mDataList.get(groupIndex);
		holder.groupTitleTv.setText(shopEntity.getShopName());
		holder.groupCheckbox.setChecked(shopEntity.isSelect());
		
		return convertView;
	}
	
	/**
	 * 处理组的全选/全不选
	 */
	private void groupAllSelect(int groupPosition, boolean isSelect){
		ArrayList<GoodsEntity> goodsList = mDataList.get(groupPosition).getGoodsList();
		for(GoodsEntity entity : goodsList){
			entity.setSelect(isSelect);
		}
		this.notifyDataSetChanged();
	}
	
	/**
	 * 子项选中状态造成组checkbox选中状态变化的关联处理
	 */
	private void childSelectAssociatedHanlder(int groupPosition){
		boolean groupIsSelect = true;
		ArrayList<GoodsEntity> goodsList = mDataList.get(groupPosition).getGoodsList();
		for(GoodsEntity entity : goodsList){
			if(!entity.isSelect()){
				groupIsSelect = false;
				break;
			}
		}
		mDataList.get(groupPosition).setSelect(groupIsSelect);
		this.notifyDataSetChanged();
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}
	
	class GroupViewHolder{
		TextView groupTitleTv;
		CheckBox groupCheckbox;
	}
	
	class ChildViewHolder{
		TextView childTitleTv;
		CheckBox childCheckbox;
	}
}
